#pragma once
#include "Thread.hpp"
#include <vector>
#include <queue>
#include "Log.hpp"
#include "Thread.hpp"
#include "Cond.hpp"
#include "Mutex.hpp"

namespace MyThreadPool
{
    using namespace MyCond;
    using namespace MyLog;
    using namespace MyMutex;
    using namespace MyThread;
    static int dnum = 5;
    template <typename T>
    class ThreadPool
    {
    private:
        ThreadPool(const int num = dnum)
            : _num(num), _isrunning(false), _sleepernum(0)
        {
            for (int i = 0; i < _num; i++)
            {
                _threads.emplace_back(
                    [this]()
                    {
                        HandlerTask();
                    });
            }
        }

        void Start()
        {
            if (_isrunning)
                return;
            _isrunning = true;
            for (auto &thread : _threads)
            {
                thread.Start();
                LOG(Loglevel::DEBUG) << "ThreadPool Start!";
            }
        }

        void WakeupAll()
        {
            LockGuard nowlock(_mutex);
            if (_sleepernum)
                _cond.NotiAll();
            LOG(Loglevel::INFO) << "唤醒所有线程";
        }

        void WakeupOne()
        {
            _cond.Notify();
            LOG(Loglevel::INFO) << "唤醒一个线程";
        }

        ThreadPool<T>(const ThreadPool<T> &) = delete;
        ThreadPool<T> operator=(const ThreadPool<T> &) = delete;

    public:
        static ThreadPool<T>* GetInstance()
        {
            if(_ins==nullptr)
            {
                LockGuard nowlock(_lock);
                LOG(Loglevel::INFO) << "获取单例";
                if(_ins ==nullptr)
                {
                    _ins = new ThreadPool<T>();
                    _ins->Start();
                    LOG(Loglevel::INFO) <<"首次获取单例，创建中...";
                }
            }
            return _ins;
        }

        void Stop()
        {
            if(!_isrunning)
                return;
            _isrunning = false;

            WakeupAll();
            LOG(Loglevel::INFO) << "ThreadPool Exit!";
        }

        void Wait()
        {
            for (auto &thread : _threads)
            {
                thread.Join();
            }
        }

        void HandlerTask()
        {
            char name[128];
            pthread_getname_np(pthread_self(), name, sizeof(name));
            while (true)
            {
                T t;
                {
                    LockGuard lockg(_mutex);
                    while (_tq.empty()&&_isrunning)
                    {
                        _sleepernum++;
                        _cond.Wait(_mutex);
                        _sleepernum--;
                    }

                    if(!_isrunning&&_tq.empty())
                    {
                        LOG(Loglevel::INFO) << name <<"退出，线程池退出&&任务队列为空";
                        break;
                    }

                    LOG(Loglevel::DEBUG) << name << " is running ";
                    t = _tq.front();
                    _tq.pop();
                }
                t();
            }
        }

        bool Enqueue(const T &in)
        {
            bool ret = false;
            if (_isrunning)
            {
                LockGuard lockg(_mutex);
                _tq.push(in);
                if(_threads.size()==_sleepernum)
                    WakeupOne();
                return true;
            }
            return false;
        }

        ~ThreadPool() {}

    private:
        std::vector<Thread> _threads;
        int _num;
        std::queue<T> _tq;
        int _sleepernum;
        Cond _cond;
        Mutex _mutex;
        bool _isrunning;

        static ThreadPool<T> *_ins;
        static Mutex _lock;
    };
    template <typename T>
    ThreadPool<T> *ThreadPool<T>::_ins = nullptr;

    template <typename T>
    Mutex ThreadPool<T>::_lock;

}
